//
//  CaseManagerViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "CaseManagerViewController.h"
#import "CaseManagerCell.h"
#import "CaseManagerContentController.h"
#import "CaseManagerModel.h"

@interface CaseManagerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) UITableView *tableView;
//记录tableView的行数
@property (nonatomic,assign)NSInteger rowNum;

//be used to save picture by array
@property (nonatomic,strong) NSMutableArray *arrPicture;

//warning model
//be used to save data by array
@property (nonatomic,strong) CaseManagerModel *model;
@property (nonatomic,strong) NSMutableArray<CaseManagerModel *> *arrayList;





//结束接档的数组
@property (nonatomic,strong) NSMutableArray<CaseManagerModel *> *arrModel;

//存储上次归档的数组,进行当前页面内值传递


@end

static NSString *cellCase = @"cellCase";
@implementation CaseManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化数组
    _arrayList = [NSMutableArray array];
   
    _arrModel = [NSMutableArray array];
    //路径  //从沙盒中取出数据
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.plist"];
    //反归档 解档
    _arrModel =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
     [_arrModel enumerateObjectsUsingBlock:^(CaseManagerModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         [_arrayList addObject:obj];
     }];
    
    //调用初始化方法
    [self setupUI];
}

#pragma mark -
#pragma mark - 初始化界面
-(void)setupUI
{
    //创建button
    UIButton *button = [UIButton buttonWithType:0];
    
    //添加
    [self.view addSubview:button];
    
    //设置属性
    [button setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    [button setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    //约束
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-20);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(35);
    }];
    //监听事件
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    _tableView = tableView;
    //添加
    [self.view addSubview:tableView];
    //代理
    tableView.delegate = self;
    tableView.dataSource = self;
    
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(64);
        make.bottom.mas_equalTo(button.mas_topMargin).mas_offset(-20);
    }];
    //行高
    tableView.rowHeight = 200;
    
    //注册cell
    [tableView registerNib:[UINib nibWithNibName:@"CaseManagerCell" bundle:nil] forCellReuseIdentifier:cellCase];
    
}
#pragma mark -
#pragma mark - add按钮监听方法
-(void)buttonClick:(UIButton *)button
{
    //由UIStoryboard创建VC
    CaseManagerContentController *contentVC = [[UIStoryboard storyboardWithName:@"CaseManagerContent" bundle:nil] instantiateViewControllerWithIdentifier:@"content"];
    [self.navigationController pushViewController:contentVC animated:YES];
    //增加病例传递过来的数据
    contentVC.BlockContent = ^(NSDictionary *dicContent){
           NSLog(@"%@",dicContent);
        
        _model = [CaseManagerModel yy_modelWithDictionary:dicContent];
        //将模型添加到数组
        [_arrayList addObject:_model];
        //刷新数据
        [self.tableView reloadData];
        /****************************************************/
       //对传递过来的数据,进行归档.归档的是数组_arrayList
        //路径
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.plist"];
        
        //2.归档
        //1.归档的该对象 必须遵守编码协议 NSCoding
        [NSKeyedArchiver archiveRootObject:_arrayList.copy toFile:filePath];
        /****************************************************/
    };
    
   
    
    
    
}

#pragma mark -
#pragma mark - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CaseManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellCase forIndexPath:indexPath];
    /****************************************************/
    //接收block
    cell.BlockDeleteCell = ^(CaseManagerCell *deleteCell){
        
        //获取按钮点击的那一行的row
        NSIndexPath *indexPath = [self.tableView indexPathForCell:deleteCell];
        //删除对应下标的cell
        [_arrayList removeObjectAtIndex:indexPath.row];
        //重新进行存档
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.plist"];
        [NSKeyedArchiver archiveRootObject:_arrayList.copy toFile:filePath];
        //刷新数据
        [self.tableView reloadData];
    };
    
    cell.model = _arrayList[indexPath.row];
    
    return cell;
}


#pragma mark -
#pragma mark - 选中cell时候调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据indexPath获取cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //取消选中状态
    [cell setSelected:NO];
}


@end
